package com.jiuyin.dianjing.ui.fragment.team;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.api.constant.ApiConstant;
import com.jiuyin.dianjing.ui.activity.team.TeamApplyListActivity;
import com.jiuyin.dianjing.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_APPLY_LIST;

/**
 *
 */
public class TeamManageDialogFragment extends BottomSheetDialogFragment {

    private static final String ARG_POSITION = "item_count";
    private static final String ARG_TEAM_ID = "team_id";
    @BindView(R.id.tv_apply)
    View tvApply;
    @BindView(R.id.tv_give_up)
    View tvGiveUp;
    @BindView(R.id.tv_set_manager)
    View tvSetManager;
    @BindView(R.id.tv_cancel)
    View tvCancel;
    private int mJob;
    private String teamId;
    private ItemClickListener itemClickListener;
    private Unbinder mUnBinder;

    /**
     * @param job    职位
     * @param teamId
     * @return
     */
    public static TeamManageDialogFragment newInstance(int job, String teamId) {
        final TeamManageDialogFragment fragment = new TeamManageDialogFragment();
        final Bundle args = new Bundle();
        args.putInt(ARG_POSITION, job);
        args.putString(ARG_TEAM_ID, teamId);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_item_list_dialog_list_dialog, container, false);
        mUnBinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        mJob = getArguments().getInt(ARG_POSITION);
        switch (mJob) {
            case ApiConstant.VALUE_PART_MANAGER:
                tvSetManager.setVisibility(View.GONE);
                tvGiveUp.setVisibility(View.GONE);
                break;
            case ApiConstant.VALUE_PART_CAPTION:
                break;
            default:
                tvSetManager.setVisibility(View.GONE);
                tvGiveUp.setVisibility(View.GONE);
                tvApply.setVisibility(View.GONE);
                break;
        }
    }

    @OnClick({R.id.tv_set_manager, R.id.tv_give_up, R.id.tv_cancel, R.id.tv_apply})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_apply:
                ToastUtil.showShort("入队申请");
                teamId = getArguments().getString(ARG_TEAM_ID);
                Intent intent = new Intent(getActivity(), TeamApplyListActivity.class);
                intent.putExtra("teamId", teamId);
                intent.putExtra(KEY_APPLY_LIST, "1");
                startActivity(intent);
                break;
            case R.id.tv_set_manager:
                if (itemClickListener != null) {
                    itemClickListener.onSetManagerClick();
                }
                break;
            case R.id.tv_give_up:
                if (itemClickListener != null) {
                    itemClickListener.onGiveUpClick();
                }
                break;
            case R.id.tv_cancel:
                break;
        }
        dismiss();
    }

    public TeamManageDialogFragment setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
        return this;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnBinder.unbind();
    }

    public interface ItemClickListener {
        void onSetManagerClick();

        void onGiveUpClick();

    }
}
